<?php defined('SYSPATH') or die('No direct script access.');

// Статьи
class Controller_Admin_Articles extends Controller_Admin_Base
{
    /*
    * Выводим все с паджинацией и возможностью редактирования
    */
	public function action_index()
	{        
        // Запросы на удаление статей
        if ($this->request->method() == HTTP_Request::POST)
        {            
            $action = $this->request->post('action');
            
            if($action == 'delete_articles')
            {                
                $articles = $this->request->post('articles') ? $this->request->post('articles') : null;
                
                if($articles)
                {
                    foreach($articles as $article_id)
                    {
                        $this->article_model->delete_article($article_id);
                    }
                    
                    $message = 'Удалили'; 
                }
            }
            
            Request::current()->redirect($this->request->referrer());
        }
        
        $subcategory_id = $this->request->param('id') ?  $this->request->param('id') : null;
        
        $page = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $count = $this->settings['articles_per_page'];
        $offset = ($page - 1)*$count;
        
        $articles = $this->article_model->get_articles($count, $offset, $subcategory_id);
        
        $url   = $subcategory_id ? '/admin/articles/index/'.$subcategory_id.'/' : '/admin/articles/';       
        $pages = array(
            'current'  => $page,
            'total'    => $this->article_model->get_count_articles($subcategory_id),
            'per_page' => $count,
            'url'      => $url
        );
        
        $title = 'Статьи';
        $content = new View('admin/articles/index_tpl');
        $content->articles = $articles;
        $content->pages    = $pages;
        
        
        $menu = array(
            'menu'    => 2,
            'submenu' => 22
        );
        
        $this->print_template($title, $content, $menu);
	}
    
    public function action_published()
    {
        $subcategory_id = $this->request->param('id') ?  $this->request->param('id') : null;
        
        $page = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $count = $this->settings['articles_per_page'];
        $offset = ($page - 1)*$count;
        
        $articles = $this->article_model->get_published_articles($count, $offset, $subcategory_id);
        
        $url   = $subcategory_id ? '/admin/articles/published/'.$subcategory_id.'/' : '/admin/articles/published/';       
        $pages = array(
            'current'  => $page,
            'total'    => $this->article_model->get_count_published_articles($subcategory_id),
            'per_page' => $count,
            'url'      => $url
        );
        
        $title = 'Статьи';
        $content = new View('admin/articles/index_tpl');
        $content->articles = $articles;
        $content->pages    = $pages;
        
        
        $menu = array(
            'menu'    => 2,
            'submenu' => 23
        );
        
        $this->print_template($title, $content, $menu); 
    }
    
    public function action_unpublished()
    {
        $subcategory_id = $this->request->param('id') ?  $this->request->param('id') : null;
        
        $page = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $count = $this->settings['articles_per_page'];
        $offset = ($page - 1)*$count;
        
        $articles = $this->article_model->get_unpublished_articles($count, $offset, $subcategory_id);
        
        $url   = $subcategory_id ? '/admin/articles/unpublished/'.$subcategory_id.'/' : '/admin/articles/unpublished/';       
        $pages = array(
            'current'  => $page,
            'total'    => $this->article_model->get_count_unpublished_articles($subcategory_id),
            'per_page' => $count,
            'url'      => $url
        );
        
        $title = 'Статьи';
        $content = new View('admin/articles/index_tpl');
        $content->articles = $articles;
        $content->pages    = $pages;
        
        
        $menu = array(
            'menu'    => 2,
            'submenu' => 24
        );
        
        $this->print_template($title, $content, $menu); 
    }
	
    /*
    * Добавление, ПОСТ запрос прилетает сюда же
    * редирктим на index
    */
	public function action_add()
	{
        if ($this->request->method() == HTTP_Request::POST)
        {            
            $title           = $this->request->post('title') ? trim($this->request->post('title')) : null;
            $subcategory_id  = $this->request->post('subcategory_id') ? (int)$this->request->post('subcategory_id') : null;
            $text            = $this->request->post('text') ? trim($this->request->post('text')) : null;
            $description     = $this->request->post('description') ? trim($this->request->post('description')) : null;                      
            $date            = strtotime($this->request->post('date'));
            
            if($title && $subcategory_id && $text)
            {
                $image = $this->main_model->upload_image('image', 'media/uploads/articles/', 300);
                
                $user = Auth::instance()->get_user();
                
                $article_id = $this->article_model->add_article($title, $subcategory_id, $text, $date, $image, $description, $user->id);
                
                Request::current()->redirect('admin/articles');
            }
        }
        
        $categories = $this->category_model->get_full_categories();
        
        $title = 'Добавление Статьи';
        
        $content = new View('admin/articles/add_tpl');
        $content->categories = $categories;
                
        $menu = array(
            'menu'    => 2,
            'submenu' => 21
        );
        
        $this->print_template($title, $content, $menu);
	}
	
    /*
    * Редактирование, потом редиректим на реферрер
    */
	public function action_edit()
	{
        $article_id = $this->request->param('id');
        
        $message = null;

        if ($this->request->method() == HTTP_Request::POST)
        {            
            $title           = $this->request->post('title') ? trim($this->request->post('title')) : null;
            $subcategory_id  = $this->request->post('subcategory_id') ? (int)$this->request->post('subcategory_id') : null;
            $text            = $this->request->post('text') ? trim($this->request->post('text')) : null;
            $description     = $this->request->post('description') ? trim($this->request->post('description')) : null;                      
            $date            = strtotime($this->request->post('date'));
            
            if($title && $subcategory_id && $text)
            {
                $image = $this->main_model->upload_image('image', 'media/uploads/articles/', 300);
                
                $user = Auth::instance()->get_user();
                
                $this->article_model->update_article($title, $subcategory_id, $text, $date, $image, $description, $article_id);
                
                $message = 'Отредактировали успешно!';
                
                //Request::current()->redirect(Request::referrer());
            }
        }
        
        $article    = $this->article_model->get_article($article_id);
        $categories = $this->category_model->get_full_categories();
        
		$title = $article['title'].' - Редактирование';
        
        $content = new View('admin/articles/edit_tpl');
        $content->categories = $categories;
        $content->article    = $article;
        $content->message    = $message;
        
        $menu = array(
            'menu'    => 2,
            'submenu' => 20
        );
        
        $this->print_template($title, $content, $menu);
	}
    
    /*
    * Удаление, потом редиректим на реферрер
    */
    public function action_delete()
    {
        $article_id = $this->request->param('id');
        
        if($article_id)
            $this->article_model->delete_article($article_id);
            
        Request::current()->redirect($this->request->referrer());
    }
    
    // Comments
    public function action_comments()
    {
        $message = null;
        
        $article_id = $this->request->param('id');
        
        if ($this->request->method() == HTTP_Request::POST)
        {            
            $action = $this->request->post('action');
            
            if($action == 'edit_comment')
            {
                $subject    = $this->request->post('subject') ? trim($this->request->post('subject')) : null;
                $comment    = $this->request->post('comment') ? trim($this->request->post('comment')) : null;
                $comment_id = $this->request->post('comment_id') ? (int)$this->request->post('comment_id'): null;

                if($comment && $comment_id)
                {
                    $this->article_comment_model->update_comment($comment, $subject, $comment_id);
                    $message = 'Успешно отредактировали!'; 
                }
            }
            else if($action == 'delete_comments')
            {                
                $comments = $this->request->post('comments') ? $this->request->post('comments') : null;
                
                if($comments)
                {
                    foreach($comments as $comment_id)
                    {
                        $this->article_comment_model->delete_comment($comment_id);
                    }
                    
                    $message = 'Удалили'; 
                }
            }
        }
        
        $article  = $this->article_model->get_article($article_id);
        $comments = $this->article_comment_model->get_comments($article_id, true);
        
        $title = 'Комментарии';
        $content = new View('admin/comments/comments_tpl');
        $content->article  = $article;
        $content->comments = $comments;
        $content->message  = $message;
        
        
        $menu = array(
            'menu'    => 2,
            'submenu' => 20
        );
        
        $this->print_template($title, $content, $menu);
    }
    
    public function action_delete_comment()
    {
        $comment_id = $this->request->param('id');
        
        if($comment_id)
            $this->article_comment_model->delete_comment($comment_id);
            
        Request::current()->redirect($this->request->referrer());
    }

} // End Admin_Articles
